<?php if (!defined("_ECRIRE_INC_VERSION")) return;
include_spip('functions/pdk_common'); 
function formulaires_ecatalogue_article_similar_traiter_dist($id_article) {
	$result = array();
    
    $mdl = pdk_load_class('EcatArticleSimilarModel','classes');    
    try {
    
    if ($selected_articles=_request('selected_articles')) {
        if (lire_config('ecatalogue/articles_select')=='enhanced') {
            include_spip('formulaires/selecteur/generique_fonctions');
            $selected_articles = picker_selected(_request('selected_articles'), 'article');    
        }
        if ($selected_articles) {
            $data = array('id_article'=>$id_article);
            foreach ($selected_articles as $id_similar) {
                if ($id_similar == $id_article) continue;
                $data['id_similar'] = $id_similar;
                $where = array('id_article='.$data['id_article'], 'id_similar='.$data['id_similar']);
                // If such link doesn't exist yet, create it
                if ($mdl->get_total($where)==0)
                    $mdl->insert($data);
                if ($mdl->get_total($where)==0)
                   throw new Exception("Insert article similar fail!"); 
            }
        }    
    }
    
    if ($id_similar_delete = _request('id_similar_delete')) {
        $where = array('id_article='.$id_article, 'id_similar='.$id_similar_delete);        
        $mdl->delete($where);
        if ($mdl->get_total($where)!=0)
            throw new Exception("Delete article similar fail!");
    }
    
        $result['message_ok'] = _T('ecatalogue:form_saved');
    } catch(Exception $e) {
        $result['message_erreur'] = _T('ecatalogue:form_error');
    }
    
    set_request('id_similar_delete');
    
	return $result;
}
?>